
****************************************************************
* Authors: Henrik Andersson & Sirus Dehdari
* This script creates weights for the translation of 2006/2010
* precincts into 2014 precincts
****************************************************************

clear all

cd "E:\ProjData\IntegrationSD\"


* Import raw data:
import delimited "raw\valdistrikt10_14.csv", delimiter(";") clear

* Replacing comma as decimal:
destring totbef, replace dpcomma
destring area_geo, replace dpcomma

* The precinct codes are numeric, which means that "0" disappears. We
* instead transform to string and add "0" to some codes:
foreach var of varlist lkfv vd{
tostring `var', replace
replace `var' = "0" + `var' if strlen(`var') < 8
}

* A area_geo equal to 0.01 means that a population square has not been divided.
* We multiply this variable by 100 to get proper shares:
replace area_geo = area_geo * 100
replace area_geo = 1 if area_geo > 1

* We multiply the population square shares by the square total population:
replace totbef = area_geo*totbef

* We collapse square population on 2010 and 2014 precinct combinations:
collapse (sum) totbef, by(vd lkfv)

save temp\vd_2010_2014, replace

* Next, we collapse on only 2010 precincts.
collapse (sum) totbef, by(lkfv)

rename totbef whole_pop

* We merge the data on total population for each 2010 precinct with the
* data on population in each 2010/2014 precinct combinations.
merge 1:m lkfv using temp\vd_2010_2014
drop _merge

* By dividing the population for each precinct combination with the total 
* population of each 2010 precinct, we compute how much of each combination,
* in terms of 2010 precincts, correspond to 2014 precincts.
gen share = totbef/whole_pop

rename lkfv precinct2010
rename vd precinct2014

save temp\vd_2010_2014_shares, replace



*** The following code does the same thing as above, but for 2006/2014:
import delimited E:\ProjData\IntegrationSD\raw\valdistrikt06_14.csv, delimiter(";") clear

destring totbef, replace dpcomma
destring area_geo, replace dpcomma
foreach var of varlist lkfv vd{
tostring `var', replace
replace `var' = "0" + `var' if strlen(`var') < 8
}
replace area_geo = area_geo * 100
replace area_geo = 1 if area_geo > 1

replace totbef = area_geo*totbef
collapse (sum) totbef, by(vd lkfv)
save temp\vd_2006_2014, replace

collapse (sum) totbef, by(lkfv)
rename totbef whole_pop
merge 1:m lkfv using temp\vd_2006_2014
drop _merge

gen share = totbef/whole_pop
rename lkfv precinct2006
rename vd precinct2014
save temp\vd_2006_2014_shares, replace

 